package com.iot.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.iot.commons.ResponseResult;
import com.iot.model.vo.AuthInfoVo;
import com.iot.model.vo.OSSVo;
import com.iot.utils.OSSUtils;
import com.iot.utils.ResponseResultUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 
 * @author: dh
 * @date: 2024-04-20 23:39:54
 */
@RestController
@RequestMapping("/oss")
@RequiredArgsConstructor
public class OSSController {

    /**
     * 文件上传
     */
    @PostMapping(value = "/upload")
    public ResponseResult upload(MultipartFile file) {
        AuthInfoVo authInfoEntity = (AuthInfoVo) StpUtil.getSession().get("loginInfo");
        if (null == authInfoEntity) {
            return ResponseResultUtil.authInfoFailure();
        }
        String fileUrl = OSSUtils.uploadFile(file, authInfoEntity.getId());
        OSSVo vo = new OSSVo();
        vo.setFileUrl(fileUrl);
        return ResponseResultUtil.success(vo);
    }

}
